<?php namespace api\Controller\V1;

use Cml\Controller;
use Cml\View;
use api\Server\ResponseServer;

class CommonController extends Controller
{
    protected $requestData = array();

    public function __construct(&$requestData = array())
    {
        $this->requestData = $requestData;
    }

    /**
     * 渲染json
     *
     * @param $code
     * @param $msg
     * @param string $data
     */
    protected function renderJson($code = 0, $msg = '', &$data = array())
    {
        ResponseServer::renderJsonWithLog($code, $msg, $data, $this->requestData);
    }

    /**
     * 上锁
     *
     * @param string $key
     *
     * @return bool
     */
    protected function lockWait($key)
    {
        $i = 0;
        do {
            if (++$i >= 3) {
                return false;
            }
            usleep(2000);
        } while (!$this->locker()->lock($key));

        return true;
    }

    /**
     * 解锁
     *
     * @param int $id
     */
    protected function unLockWait($key)
    {
        $this->locker()->unlock($key);
    }
}